﻿using AutoMapper;
using B.S.BaseData.Domain;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infratructure.Interfares;
using B.S.BaseData.Write.Api.Application.Command;
using MediatR;

namespace B.S.BaseData.Write.Api.Application.CommandHander
{
    public class DelRecodeCommandHander : IRequestHandler<DelRecodeCommand, ApiResult<int>>
    {

        private readonly IMapper mapper;
        private readonly IBaseRequsitory<WF_Recode> recodeRequsitory;
        private readonly IBaseRequsitory<Leave> leaveRequsitory;
        private readonly IBaseRequsitory<WF_Instafre> instaresRequsitory;

        public DelRecodeCommandHander(IMapper mapper, IBaseRequsitory<WF_Recode> recodeRequsitory, IBaseRequsitory<Leave> leaveRequsitory, IBaseRequsitory<WF_Instafre> instaresRequsitory)
        {
            this.mapper = mapper;
            this.recodeRequsitory = recodeRequsitory;
            this.leaveRequsitory = leaveRequsitory;
            this.instaresRequsitory = instaresRequsitory;
        }

        /// <summary>
        /// 删除处理
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<ApiResult<int>> Handle(DelRecodeCommand request, CancellationToken cancellationToken)
        {
            ApiResult<int> result = new ApiResult<int>();
            result.Code = ApiEnum.删除成功;
            result.Msg = ApiEnum.删除成功.ToString();

            var entity = mapper.Map<WF_Recode>(request);

            var list = leaveRequsitory.GetAllAsync().Where(x => x.LeaveId == request.LeaveId).FirstOrDefault();

            leaveRequsitory.Del(list);

            var recode = recodeRequsitory.GetAllAsync().Where(x => x.LeaveId == request.LeaveId).FirstOrDefault();

            recodeRequsitory.Del(recode);

            var instares = instaresRequsitory.GetAllAsync().Where(x => x.leaveId == request.LeaveId).FirstOrDefault();

            instaresRequsitory.Del(instares);

            return Task.FromResult(result);

        }
    }
}
